
import 'package:fluent_ui/fluent_ui.dart' as flu;
import 'package:syria_sports/core/app_export.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/presentation/tools/add_tool/bloc/add_tool_page_screen.dart';
import '../../../../dialogs/delete_player_confirmation.dart';
import '../../../exercises/exercises_page/models/exercise.dart';
import '../../tools_page/models/tool.dart';
import '../bloc/exercise_tools_cubit.dart';



class GymSportItemView extends flu.StatefulWidget {
  final ToolModel tool;
  final bool isSelected;
  final bool selectOnTap;
  final Function? onTapped;
  final Exercise exercise;

  const GymSportItemView({
    super.key,
    required this.tool,
    this.isSelected = false,
    this.selectOnTap = false,
    this.onTapped,
    required this.exercise,
  });

  @override
  flu.State<GymSportItemView> createState() => _GymSportItemViewState();
}

class _GymSportItemViewState extends flu.State<GymSportItemView> {
  final controller = flu.FlyoutController();

  final attachKey = GlobalKey();

  final menuController = flu.FlyoutController();

  final menuAttachKey = GlobalKey();

  final contextController = flu.FlyoutController();

  final contextAttachKey = flu.GlobalKey();

  bool barrierDismissible = true;

  bool dismissOnPointerMoveAway = false;

  bool dismissWithEsc = true;

  flu.FlyoutPlacementMode placementMode = flu.FlyoutPlacementMode.topCenter;

  @override
  void initState() {
    super.initState();
    controller.addListener(() {
      if (mounted) setState(() {});
    });
  }

  @override
  void dispose() {
    controller.dispose();
    menuController.dispose();
    contextController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var cubit = ExerciseToolsCubit.get(context);

    return flu.FluentTheme(
      data: flu.FluentThemeData(
        brightness: flu.Brightness.dark,
        visualDensity: flu.VisualDensity.standard,
        focusTheme: const flu.FocusThemeData(
          glowFactor: 2.0,
        ),
      ),
      child: flu.FlyoutTarget(
        key: menuAttachKey,
        controller: menuController,
        child: InkWell(
          child: Stack(
            children: [
              Card(
                color: Theme.of(context).colorScheme.secondary,
                child: Container(
                  padding: const EdgeInsets.all(4),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      if (widget.tool.image != null)
                        Image.network(
                          widget.tool.image!,
                          height: 45,
                        )
                      else
                        Image.asset(
                          ImageConstant.imgLogo,
                          height: 45,
                        ),
                      Expanded(
                        child: Center(
                          child: Text(
                            widget.tool.toolName,
                            maxLines: 2,
                            textAlign: TextAlign.center,
                            style: theme.textTheme.bodyMedium,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              if (cubit.selectMode)
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Checkbox(
                      value: widget.isSelected,
                      onChanged: (value) {
                        cubit.add(
                          SelectToolEvent(
                            tool: widget.tool,
                            isSelected: value ?? false,
                          ),
                        );
                      },
                    ),
                  ],
                ),
            ],
          ),
          onTap: () {
            if (widget.selectOnTap) {
              if (cubit.selectMode) {
                cubit.add(
                  SelectToolEvent(
                    tool: widget.tool,
                    isSelected: !widget.isSelected,
                  ),
                );
              } else {
                cubit.add(SelectModeEvent(mode: true, sport: widget.tool));
              }
            } else {
              if (widget.onTapped?.call() == null) {
                /*Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => AddSportPageScreen(
                          sport: sport,
                        )));*/
              }
            }
          },
          onLongPress: () {
            if (!cubit.selectMode) {
              cubit.add(SelectModeEvent(mode: true, sport: widget.tool));
            }
            onLongPress(context,cubit);
          },
          onSecondaryTapUp: (td) {
            onLongPress(context,cubit);
          },
        ),
      ),
    );

  }

  onLongPress (BuildContext context ,ExerciseToolsCubit cubit){
    menuController.showFlyout(
      autoModeConfiguration: flu.FlyoutAutoConfiguration(
        preferredMode: placementMode,
      ),
      barrierDismissible: barrierDismissible,
      dismissOnPointerMoveAway: dismissOnPointerMoveAway,
      dismissWithEsc: dismissWithEsc,
      //navigatorKey: rootNavigatorKey.currentState,
      builder: (context1) {
        return flu.FluentTheme(
          data: flu.FluentThemeData(
            brightness: flu.Brightness.dark,
            visualDensity: flu.VisualDensity.standard,
            focusTheme: const flu.FocusThemeData(
              glowFactor: 2.0,
            ),
          ),
          child: flu.MenuFlyout(items: [

            flu.MenuFlyoutItem(
              leading: const Icon(flu.FluentIcons.edit),
              text: const Text('تعديل'),
              onPressed:() {
                //print('hi');
                flu.Flyout.of(context1).close();
                Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => AddToolPageScreen(
                          tool: widget.tool,
                        )));
              },
            ),
            flu.MenuFlyoutItem(
              leading: const flu.Icon(flu.FluentIcons.refresh),
              text: const flu.Text('تحديث'),
              onPressed: (){
                cubit.add(ExerciseToolsInitialEvent(exercise: widget.exercise));
                flu.Flyout.of(context1).close();
              },
            ),
            flu.MenuFlyoutItem(
              leading: const flu.Icon(flu.FluentIcons.delete),
              text: const flu.Text('حذف'),
              onPressed:() {
                showDeleteConfirmation(context,widget.tool,cubit);
                flu.Flyout.of(context1).close();
              },
            ),
            const flu.MenuFlyoutSeparator(),
            flu.MenuFlyoutItem(
              text: const Text('تحديد'),
              leading: const flu.Icon(flu.FluentIcons.multi_select),
              onPressed: (){
                if (cubit.selectMode) {
                  cubit.add(
                    SelectToolEvent(
                      tool: widget.tool,
                      isSelected: !widget.isSelected,
                    ),
                  );
                } else {
                  cubit.add(SelectModeEvent(mode: true, sport: widget.tool));
                }
                flu.Flyout.of(context1).close();
              },
            ),
            flu.MenuFlyoutItem(
              text: const Text('تحديد الكل'),
              leading: const flu.Icon(flu.FluentIcons.select_all),
              onPressed: (){
                flu.Flyout.of(context1).close();
                cubit.add(SelectAllToolsEvent());
              },
            ),
            flu.MenuFlyoutItem(
              text: const Text('إلغاء التحديد'),
              leading: const flu.Icon(flu.FluentIcons.clear_selection),
              onPressed: (){
                flu.Flyout.of(context1).close();
                cubit.add(UnSelectAllToolsEvent());
              },
            ),
          ]),
        );
      },
    );
  }

  void showDeleteConfirmation(BuildContext context, ToolModel tool,ExerciseToolsCubit cubit) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: tool.toolName,
          titleUser: 'الاداة',
          onConfirm: () {
            cubit.add(DeleteToolsFromExerciseEvent(tool: tool));
          },
        );
      },
    );
  }
}
